package com.atom.module.mvvm.util

import android.util.SparseArray
import java.util.*

enum class DataRegistry {
    ;

    companion object {
        private val data: SparseArray<Any> = SparseArray()
        private val random: Random = Random()

        fun key(suffix: String = ""): String {
            return "DataRegistry_$suffix"
        }

        /**
         * Registers the given object in the registry.
         */
        fun register(obj: Any): Int {
            val index = random.nextInt()
            data.put(index, obj)
            return index
        }

        fun retrieve(index: Int): Any {
            return retrieve(index, false)
        }

        fun retrieve(index: Int, keep: Boolean): Any {
            val obj = data[index]
            if (!keep) data.remove(index)
            return obj
        }
    }
}